﻿using System;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingLayoutForm : DevExpress.XtraLayout.Customization.UserCustomizationForm
    {
        private DataLayoutControlPlus target;
        public FrmSettingLayoutForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        private void XCIUserCustomizationForm_Load(object sender, EventArgs e)
        {
            target = (DataLayoutControlPlus)OwnerControl;
            editId.Text = target.Id;

            TabDelayLoad.Init(tabPaneForm, name =>
            {
                switch (name)
                {
                    case nameof(tabTemplate):
                        gridTemplate1.Init(target.SettingManager, "layout_:_");
                        break;
                    case nameof(tabAdvanced):
                        gridAdvanced1.SelectedObject = target.Root;
                        break;
                }
            });
            tabGeneral.BackColor = tabAdvanced.BackColor = this.BackColor;
        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表单配置吗？重置后此表单的配置会全部删除") != DialogResult.Yes) return;

            await target.SettingManager.DeleteAllAsync();
            MessageBoxHelper.ShowMessage("重新打开页面后生效");
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表单默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await target.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复表格默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前表单配置设为默认值吗？") != DialogResult.Yes) return;

            await target.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await target.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
